﻿using System;
using System.Collections;
using System.Collections.Generic;
using Consts;
using Dto;
using Newtonsoft.Json;
using UI;
using UI.Msg;
using UnityEngine;
using UnityEngine.UI;

public class StartPanel : UIBase
{
    private Button btnLogin;

    private Button btnClose;

    private InputField inputAccount;

    private InputField inputPassword;
    private PromptMsg _promptMsg;

    private void Awake()
    {
        Bind(UIEvent.START_PANEL_ACTIVE);
    }
    

    // Start is called before the first frame update
    void Start()
    {
        // Debug.Log("Start");
        btnLogin = transform.Find("BtnLogin").GetComponent<Button>();
        btnClose = transform.Find("BtnClose").GetComponent<Button>();
        inputAccount = transform.Find("InputAccount").GetComponent<InputField>();
        inputPassword = transform.Find("InputPassword").GetComponent<InputField>();
        btnLogin.onClick.AddListener(LoginClick);
        btnClose.onClick.AddListener(CloseClick);
        _promptMsg = new PromptMsg();
        
        setPanelActive(false);
    }

    // Update is called once per frame
    void Update()
    {
    }
    
    public override void Execute(int eventCode, object message)
    {
        switch (eventCode)
        {
            case UIEvent.START_PANEL_ACTIVE:
                setPanelActive((bool)message);
                break;
            default:
                break;
        }
    }
    
    public override void OnDestroy()
    {
        base.OnDestroy();
        btnLogin.onClick.RemoveListener(LoginClick);
        btnClose.onClick.RemoveListener(CloseClick);
    }

    /**
     * 登录按钮的点击事件处理
     */
    private void LoginClick()
    {
        if (string.IsNullOrEmpty(inputAccount.text))
        {
            _promptMsg.Change("登录的用户名不能为null",Color.red);
            Dispatch(AreaCode.UI,UIEvent.PROMPT_MSG,_promptMsg);
            return;
        }

        if (string.IsNullOrEmpty(inputPassword.text))
        {
            _promptMsg.Change("登录密码不能为null",Color.red);
            Dispatch(AreaCode.UI,UIEvent.PROMPT_MSG,_promptMsg);
            return;
        }
        AccountDto accountDto = new AccountDto(inputAccount.text,inputPassword.text);
        ReqBase reqBase = new ReqBase(SocketConst.ACCOUNT,AccountCodeConst.LOGIN,accountDto);
        Dispatch(AreaCode.NET,0,reqBase);
      
    }

    public void CloseClick()
    {
        Debug.Log("关闭");
        setPanelActive(false);
    }
}